package com.stk.megastack;

import java.util.ArrayList;
import java.util.Vector;

import android.R.integer;
import android.content.Context;
import android.drm.DrmStore.Action;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Game extends SurfaceView implements SurfaceHolder.Callback {
	private CanvasThread canvasthread = null;
	private final int numberSquaresWidth = 6;
	private final int numberSquaresHeight = 10;
	private int width;
	private int height;
	private int pixelHeightSquare;
	private int pixelWidthSquare;
	private Box[][] table;
	private int level;
	private int row;
	private int direction;
	private long last;
	private long elapsed;
	private ArrayList<Point> toDestroy;

	public Game(Context context, AttributeSet attrs) {
		super(context, attrs);
		getHolder().addCallback(this);
		canvasthread = new CanvasThread(getHolder(), this);
		setFocusable(true);  

		DisplayMetrics dm = context.getResources().getDisplayMetrics();	     
		width = dm.widthPixels;
		height = dm.heightPixels;
		pixelHeightSquare = (int) height / numberSquaresHeight;
		pixelWidthSquare = (int) width / numberSquaresWidth;

		table = new Box[numberSquaresWidth][numberSquaresHeight];

		for (int i=0; i < table.length; i++){
			for (int j=0; j < table[i].length; j++){
				table[i][j] = new Box(new Point(i * pixelWidthSquare, j*pixelHeightSquare));
			}
		}

		level = 1;
		direction = 1;		
		last = System.currentTimeMillis();		
		newGame(level);
	}

	private void newGame(int level){
		row = 9;
		for (int i=0; i < table.length; i++){
			for (int j=0; j < table[i].length; j++){
				table[i][j].setActive(false);
				table[i][j].setToDestroy(false);
			}
		}
		
		toDestroy = new ArrayList<Point>();
		
		elapsed = 5000 / (level*10);
		table[0][row].setActive(true);
		table[1][row].setActive(true);
		table[2][row].setActive(true);

	}
	
	public void updateGame(){
		long current = System.currentTimeMillis();
		if( current - last > elapsed){
			this.moveBoxes();
			last = current;
		}		
		
		fadeOutToDestroy();
	}

	private void moveBoxes(){
		
		
		boolean change = false;
		ArrayList<Integer> boxes = getActiveBoxes();
		
		if (direction == 1){			
			for (Integer i : boxes){
				if (i == numberSquaresWidth-1){
					change = true;					
				}
			}
		}else{
			for (Integer i : boxes){
				if (i == 0){
					change = true;
				}
			}
		}
		
		if (change){
			 direction = direction * -1;
		}
		
		if (direction == 1){
			for (Integer i : boxes){
				table[i][row].setActive(false);
			}
			for (Integer i : boxes){
				table[i+1][row].setActive(true);
			}
		}else{
			for (Integer i : boxes){
				table[i][row].setActive(false);
			}
			for (Integer i : boxes){
				table[i-1][row].setActive(true);
			}
		}
		
	}
	
	private ArrayList<Integer> getActiveBoxes(){
		ArrayList<Integer> boxes = new ArrayList<Integer>();
		for (int i = 0; i < numberSquaresWidth; i++){
			if (table[i][row].isActive()){
				boxes.add(new Integer(i));
			}			
		}	
		return boxes;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub

	}


	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if (canvasthread.getState() == Thread.State.TERMINATED)
		{
			canvasthread = new CanvasThread(getHolder(), this);  
			canvasthread.setRunning(true);
			canvasthread.start();
		}
		else
		{
			canvasthread.setRunning(true);
			canvasthread.start();
		}            
	}


	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		canvasthread.setRunning(false);
		while (retry) {
			try {
				canvasthread.join();
				retry = false;
			} catch (InterruptedException e) {
				// we will try it again and again...
			}
		}

	}



	@Override
	public void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		canvas.drawARGB(255, 120, 50, 50);		

		for (int i=0; i< numberSquaresWidth; i++){
			for (int j=0; j< numberSquaresHeight; j++){
				if (table[i][j].isToDestroy()){
					paint.setARGB(table[i][j].getAlpha(), 255, 0, 0);
				}else if (table[i][j].isActive()){
						paint.setARGB(255, 255, 0, 0);
										
				}else{
					paint.setARGB(255, 0, 0, 0);
				}
				canvas.drawRect(table[i][j].getPos().x + 1, table[i][j].getPos().y + 1, table[i][j].getPos().x + pixelWidthSquare -1, table[i][j].getPos().y + pixelHeightSquare -1, paint);
			}
		}
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN){
			if (row > 0){
				addRow();				
			}else{
				newGame(level++);
			}
		}
		return true;
		
	}

	private void addRow() {
		if (row != numberSquaresHeight-1){
			ArrayList<Integer> boxes = this.getActiveBoxes();
			for (int i : boxes){
				if (!table[i][row+1].isActive()){
					table[i][row].setActive(false);
					table[i][row].setToDestroy(true);
					toDestroy.add(new Point(i, row));
				}
			}
		}

		
		createnewRow();
		row --;
		elapsed = elapsed - (elapsed/10);
	}

	private void fadeOutToDestroy() {
		ArrayList<Point> destroyed = new ArrayList<Point>();
		for (Point p : toDestroy){			
			if (table[p.x][p.y].fadeout()){
				destroyed.add(p);
			}
		}
		toDestroy.removeAll(destroyed);
	}
	
	private void createnewRow(){
		for (int i=0;i< getActiveBoxes().size(); i++){
			table[i][row-1].setActive(true);
		}
	}

}
